package com.dmwork.vod.mq.receiver;


import com.dmwork.common.base.result.ExceptionUtils;
import com.dmwork.vod.service.VodService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class MessageReceiver {

    @Autowired
    private VodService vodService;

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(
                            value = "${spring.rabbitmq.listener.custom.receiver.message-receiver.on-batch-video-remove-message-receive.queue.name}",
                            durable = "${spring.rabbitmq.listener.custom.receiver.message-receiver.on-batch-video-remove-message-receive.queue.durable}",
                            arguments = {
                                    @Argument(
                                            name = "x-dead-letter-exchange",
                                            value = "${spring.rabbitmq.listener.custom.receiver.message-receiver.on-batch-video-remove-message-receive.x-dead-letter-exchange}"
                                    ),
                                    @Argument(
                                            name = "x-dead-letter-routing-key",
                                            value = "${spring.rabbitmq.listener.custom.receiver.message-receiver.on-batch-video-remove-message-receive.x-dead-letter-routing-key}"
                                    )
                            }
                    ),
                    exchange = @Exchange(
                            value = "${spring.rabbitmq.listener.custom.receiver.message-receiver.exchange.name}",
                            type = "${spring.rabbitmq.listener.custom.receiver.message-receiver.exchange.type}",
                            durable = "${spring.rabbitmq.listener.custom.receiver.message-receiver.exchange.durable}",
                            ignoreDeclarationExceptions = "${spring.rabbitmq.listener.custom.receiver.message-receiver.exchange.ignore-declaration-exceptions}"
                    ),
                    key = {"${spring.rabbitmq.listener.custom.receiver.message-receiver.on-batch-video-remove-message-receive.key}"}
            )
    })
    @RabbitHandler
    public void onBatchVideoRemoveMessageReceive(Message<Map<String, Object>> message, Channel channel) throws IOException {
        try {
            Map<String, Object> payload = message.getPayload();
            List<String> videoIdList= (List<String>) payload.get("videoIdList");
            vodService.removeVideoByVideoIdList(videoIdList);
            log.info("批量删除视频。。。");
            long deliveryTag= (long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
            channel.basicAck(deliveryTag, false);
        }catch (Exception e) {
            ExceptionUtils.getMessage(e);
            long deliveryTag= (long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
            channel.basicNack(deliveryTag, false, false);
        }
    }

}
